#include <STC15F2K60S2.H>
#include "Tim.h"
#include "Seg.h"
#include "Key.h"
#include "Onewire.h"
#include "Ds1302.h"
#include "stdio.h"
#include "LED.h"
//对时钟的运用和按键，温度传感器的使用
unsigned char ucLed;
unsigned long ulms;
unsigned char ucClock_Dly;
unsigned char pucSeg_Buf[9],pucSeg_Code[8],ucSeg_Pos;
unsigned int ucSeg_Dly;
unsigned char ucKey_Dly,ucKey_Old;
unsigned int uiTemp;
unsigned char ucState;
unsigned char ucSeg_Flag;
unsigned char pucRtc[3]={23,59,50};
unsigned char pucRtc1[3]={0,0,0};
unsigned char ucSetT;
unsigned int uiTemp;
unsigned char ucTemp;
unsigned char TimeS;
unsigned int Clock_Time;
unsigned char Clock_S;
void Key_Proc(void) ;
void Time_Set(unsigned char ucKey);
void Seg_Proc(void);
void Time_Set1(unsigned char ucKey);
void Time_S(void);
void main(){
	Cls_Peripheral() ;
	Timer1Init();
	Set_RTC(pucRtc);
	while(1){
		Key_Proc() ;
		Seg_Proc();
		Time_S();
}
}
void Time_1(void) interrupt 3 //定时器1中断函数
{ 
	 ulms++; 
	if(TimeS==1){
			if(++Clock_S==200){
		ucLed^=1;
		Clock_S=0;
	}
	if(++Clock_Time==5000){
		TimeS=0;
		ucLed=0;
		Clock_Time=0;
	}
	}
	 if(!(ulms % 1000)) 
	 { 
		 ucSeg_Flag^=1;
	}
	 if(++ucClock_Dly==20) ucClock_Dly=0;
	 if(++ucSeg_Dly==500) ucSeg_Dly=0;
	if(++ucKey_Dly==10) ucKey_Dly=0;
	 Seg_Disp(pucSeg_Code, ucSeg_Pos);
	if(++ucSeg_Pos==8) ucSeg_Pos=0;
	
}
void Key_Proc(void) 
{ 
	 unsigned char ucKey_Val, ucKey_Down, ucKey_Up; 
	 if(ucKey_Dly) return; 
		ucKey_Dly = 1; 
	 ucKey_Val = Key_Read(); 
	 ucKey_Down = ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Up = ~ucKey_Val & (ucKey_Old ^ ucKey_Val); 
	 ucKey_Old = ucKey_Val; 
		 if(TimeS==1){
		 if(ucKey_Down){
			 TimeS=0;
			 ucLed=0;
		 }
	 }
	 switch(ucKey_Down){
		 case 4:
			if(++ucSetT==4) ucSetT=0;
		 break;
		 case 5:
			 if(++ucState==4) ucState=0;
			break;
		 case 8:
				Time_Set(ucKey_Down);
				Time_Set1(ucKey_Down);
		 break;
	 case 9:
				Time_Set(ucKey_Down);
				Time_Set1(ucKey_Down);
		 break;
	 }
	 if(ucKey_Up==8&&ucState==0&&ucSetT==0){
		 ucTemp^=1;
	 }
}
void Time_Set(unsigned char ucKey){
	if(ucState) {
		switch(ucState){
		case 1:
			if(ucKey==9){
				if(++pucRtc[0]==24) pucRtc[0]=0;
			}
			else if(ucKey==8){
				if(--pucRtc[0]==255) pucRtc[0]=23;
			}
			break;
			case 2:
			if(ucKey==9){
				if(++pucRtc[1]==60) pucRtc[1]=0;
			}
			else if(ucKey==8){
				if(--pucRtc[1]==255) pucRtc[1]=59;
			}
			//Set_RTC(pucRtc);
			break;
			case 3:
			if(ucKey==9){
				if(++pucRtc[2]==60) pucRtc[2]=0;
			}
			else if(ucKey==8){
				if(--pucRtc[2]==255) pucRtc[2]=59;
			}
			break;
	}
	Set_RTC(pucRtc);
	}
	else if(ucKey==8&&ucSetT==0){
		ucTemp^=1;
	}
}
void Time_Set1(unsigned char ucKey){
	if(!ucSetT) return;
	switch(ucSetT){
		case 1:
			if(ucKey==9){
				if(++pucRtc1[0]==24) pucRtc1[0]=0;
			}
			else if(ucKey==8){
				if(--pucRtc1[0]==255) pucRtc1[0]=23;
			}
			break;
			case 2:
			if(ucKey==9){
				if(++pucRtc1[1]==60) pucRtc1[1]=0;
			}
			else if(ucKey==8){
				if(--pucRtc1[1]==255) pucRtc1[1]=59;
			}
			break;
			case 3:
			if(ucKey==9){
				if(++pucRtc1[2]==60) pucRtc1[2]=0;
			}
			else if(ucKey==8){
				if(--pucRtc1[2]==255) pucRtc1[2]=59;
			}
			break;
	}
}
void Seg_Proc(void){
	if(ucSeg_Dly) return;
	ucSeg_Dly=1;
	if(ucSetT){
		switch(ucSetT){
			case 1:
			if(ucSeg_Flag) 
			sprintf(pucSeg_Buf,"%02d-%02d-%02d",(unsigned int)pucRtc1[0],(unsigned int)pucRtc1[1],(unsigned int)pucRtc1[2]);
			else 
			sprintf(pucSeg_Buf,"  -%02d-%02d",(unsigned int)pucRtc1[1],(unsigned int)pucRtc1[2]);
			break;
			case 2:
			if(ucSeg_Flag) 
			sprintf(pucSeg_Buf,"%02d-%02d-%02d",(unsigned int)pucRtc1[0],(unsigned int)pucRtc1[1],(unsigned int)pucRtc1[2]);
			else 
			sprintf(pucSeg_Buf,"%02d-  -%02d",(unsigned int)pucRtc1[0],(unsigned int)pucRtc1[2]);
			break;
			case 3:
			if(ucSeg_Flag) 
			sprintf(pucSeg_Buf,"%02d-%02d-%02d",(unsigned int)pucRtc1[0],(unsigned int)pucRtc1[1],(unsigned int)pucRtc1[2]);
			else 
			sprintf(pucSeg_Buf,"%02d-%02d-  ",(unsigned int)pucRtc1[0],(unsigned int)pucRtc1[1]);
			break;
		}
	}
	else if(ucTemp){
		uiTemp=rd_temperature();
		sprintf(pucSeg_Buf,"     %02dC",uiTemp/16);
	}
	else{
		switch(ucState){
		case 0:
			Read_RTC(pucRtc) ;
			sprintf(pucSeg_Buf,"%02d-%02d-%02d",(unsigned int)pucRtc[0],(unsigned int)pucRtc[1],(unsigned int)pucRtc[2]);
		break;
		case 1:
			//Read_RTC(pucRtc) ;
			if(ucSeg_Flag) 
			sprintf(pucSeg_Buf,"%02d-%02d-%02d",(unsigned int)pucRtc[0],(unsigned int)pucRtc[1],(unsigned int)pucRtc[2]);
			else 
			sprintf(pucSeg_Buf,"  -%02d-%02d",(unsigned int)pucRtc[1],(unsigned int)pucRtc[2]);
			break;
			case 2:
			//Read_RTC(pucRtc) ;
			if(ucSeg_Flag) 
			sprintf(pucSeg_Buf,"%02d-%02d-%02d",(unsigned int)pucRtc[0],(unsigned int)pucRtc[1],(unsigned int)pucRtc[2]);
			else 
			sprintf(pucSeg_Buf,"%02d-  -%02d",(unsigned int)pucRtc[0],(unsigned int)pucRtc[2]);
			break;
			case 3:
			//Read_RTC(pucRtc) ;
			if(ucSeg_Flag) 
			sprintf(pucSeg_Buf,"%02d-%02d-%02d",(unsigned int)pucRtc[0],(unsigned int)pucRtc[1],(unsigned int)pucRtc[2]);
			else 
			sprintf(pucSeg_Buf,"%02d-%02d-  ",(unsigned int)pucRtc[0],(unsigned int)pucRtc[1]);
			break;
	}
	}
	Seg_Tran(pucSeg_Buf, pucSeg_Code);
}
void Time_S(void){
	unsigned int i;
	unsigned char count=0;
	 if(ucClock_Dly) return; 
		ucClock_Dly = 1; 
	
	for(i=0;i<3;i++){
		if(pucRtc[i]==pucRtc1[i]) count++;
	}
	if(count==3){
		TimeS=1;
	}		
	Led_Disp(ucLed);
}